package com.simen.album;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;

/**
 * 获取点击\滑动\缩放等手势
 * Created by zhangming on 16/6/29.
 */
public class GestureView extends View {
    public final static String TAG = GestureView.class.getCanonicalName();

    private GestureDetector gestureDetector;
    private ScaleGestureDetector scaleGestureDetector;

    public GestureView(Context context) {
        this(context, null);
    }

    public GestureView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GestureView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setFocusable(true);
        setClickable(true);
        gestureDetector = new GestureDetector(context, simpleOnGestureListener, null, true);
        scaleGestureDetector = new ScaleGestureDetector(context, simpleOnScaleGestureListener);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        scaleGestureDetector.onTouchEvent(event);

        if (!scaleGestureDetector.isInProgress()) {
            gestureDetector.onTouchEvent(event);
        }

        int action = event.getAction();
        switch (action & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_UP:
                return onUp(event);
        }
        return super.onTouchEvent(event);
    }

    protected boolean onUp(MotionEvent e) {
        return true;
    }

    private GestureDetector.SimpleOnGestureListener simpleOnGestureListener = new GestureDetector.SimpleOnGestureListener() {

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            return GestureView.this.onDoubleTap(e);
        }

        @Override
        public boolean onDown(MotionEvent e) {
            return GestureView.this.onDown(e);
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            return GestureView.this.onSingleTapConfirmed(e);
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            return GestureView.this.onScroll(e1, e2, distanceX, distanceY);
        }

    };

    private ScaleGestureDetector.SimpleOnScaleGestureListener simpleOnScaleGestureListener = new ScaleGestureDetector.SimpleOnScaleGestureListener() {
        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            return GestureView.this.onScale(detector);
        }

        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {
            return GestureView.this.onScaleBegin(detector);
        }

        @Override
        public void onScaleEnd(ScaleGestureDetector detector) {
            GestureView.this.onScaleEnd(detector);
        }
    };

    protected boolean onDoubleTap(MotionEvent e) {
        return false;
    }

    protected boolean onDown(MotionEvent e) {
        return false;
    }

    protected boolean onSingleTapConfirmed(MotionEvent e) {
        return false;
    }

    protected boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    protected boolean onScale(ScaleGestureDetector detector) {
        return false;
    }

    protected boolean onScaleBegin(ScaleGestureDetector detector) {
        return true;
    }

    protected void onScaleEnd(ScaleGestureDetector detector) {
    }
}
